package com.mlnx.work.service.iml;

import com.mlnx.common.utils.MyLog;
import com.mlnx.work.service.MailService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * Created by amanda.shan on 2020/12/30.
 */
@Service
public class MailServiceIml implements MailService {

    MyLog log = MyLog.getLog(getClass());

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String from;

    private ExecutorService executorService = Executors.newScheduledThreadPool(5);

    public void sendMailCode(String to, String code){

        executorService.submit(new Runnable() {
            @Override
            public void run() {
                SimpleMailMessage message = new SimpleMailMessage();
                // 发件人
                message.setFrom(from);
                // 收件人
                message.setTo(to);
                // 邮件标题
                message.setSubject("身份验证");
                // 邮件内容
                message.setText(String.format("验证码 %s，您正在进行身份验证", code));
                // 抄送人
//        message.setCc("xxx@qq.com");

                log.info("开始发送邮件:{}", to);
                mailSender.send(message);
                log.info("发送邮件成功:{}", to);
            }
        });
    }
}
